*******************************
****					   ****
****     FATA PROJECT      ****
**** (dataset preparation) ****
****					   ****
*******************************


global PathData = "~/Dropbox/FATA_fractionalization/1_Data/"
global PathRes = "~/Dropbox/FATA_fractionalization/4_Results/"

use "${PathData}FATA_Data_geocoded.dta", clear
drop in 2799
merge m:1 tribe using "${PathData}tribes_detailed.dta", nogenerate 


replace tribe_code=9 if tribe_code==48 
replace tribe_code=14 if tribe_code==50 
replace tribe_code=20 if tribe_code==24
replace tribe_code=21 if tribe_code==61
replace tribe_code=30 if tribe_code==59
replace tribe_code=38 if tribe_code==69
replace tribe_code=4 if tribe_code==42

replace tribe_name="Burki" if tribe_name=="Burki "
replace tribe_name="Muqbal" if tribe_name=="Muqbil"
replace tribe_name="Wazir" if tribe_name=="Wazir "

tab tribe_name, gen(tribe)



******************************
****     VILLAGE FRAC     ****
******************************

bys vill_code: egen p_tribe1 = mean(tribe1) 
bys vill_code: egen p_tribe2 = mean(tribe2) 
bys vill_code: egen p_tribe3 = mean(tribe3) 
bys vill_code: egen p_tribe4 = mean(tribe4) 
bys vill_code: egen p_tribe5 = mean(tribe5) 
bys vill_code: egen p_tribe6 = mean(tribe6) 
bys vill_code: egen p_tribe7 = mean(tribe7) 
bys vill_code: egen p_tribe8 = mean(tribe8) 
bys vill_code: egen p_tribe9 = mean(tribe9) 
bys vill_code: egen p_tribe10 = mean(tribe10)
bys vill_code: egen p_tribe11 = mean(tribe11)
bys vill_code: egen p_tribe12 = mean(tribe12)
bys vill_code: egen p_tribe13 = mean(tribe13)
bys vill_code: egen p_tribe14 = mean(tribe14)
bys vill_code: egen p_tribe15 = mean(tribe15)
bys vill_code: egen p_tribe16 = mean(tribe16)
bys vill_code: egen p_tribe17 = mean(tribe17)
bys vill_code: egen p_tribe18 = mean(tribe18)
bys vill_code: egen p_tribe19 = mean(tribe19)
bys vill_code: egen p_tribe20 = mean(tribe20)
bys vill_code: egen p_tribe21 = mean(tribe21)


bys vill_code: gen biggest_tribe_vill = max(p_tribe1, p_tribe2,  p_tribe3,  p_tribe4,  p_tribe5,  p_tribe6,  p_tribe7,  p_tribe8,  p_tribe9,  p_tribe10,  p_tribe11,  p_tribe12,  p_tribe13,  p_tribe14,  p_tribe15,  p_tribe16,  p_tribe17,  p_tribe18,  p_tribe19,  p_tribe20,  p_tribe21)

gen max_tribe_vill=1 if p_tribe1==biggest_tribe_vill 
replace max_tribe_vill=2 if p_tribe2==biggest_tribe_vill 
replace max_tribe_vill=3 if p_tribe3==biggest_tribe_vill 
replace max_tribe_vill=4 if p_tribe4==biggest_tribe_vill 
replace max_tribe_vill=5 if p_tribe5==biggest_tribe_vill 
replace max_tribe_vill=6 if p_tribe6==biggest_tribe_vill 
replace max_tribe_vill=7 if p_tribe7==biggest_tribe_vill 
replace max_tribe_vill=8 if p_tribe8==biggest_tribe_vill 
replace max_tribe_vill=9 if p_tribe9==biggest_tribe_vill 
replace max_tribe_vill=10 if p_tribe10==biggest_tribe_vill 
replace max_tribe_vill=11 if p_tribe11==biggest_tribe_vill 
replace max_tribe_vill=12 if p_tribe12==biggest_tribe_vill 
replace max_tribe_vill=13 if p_tribe13==biggest_tribe_vill 
replace max_tribe_vill=14 if p_tribe14==biggest_tribe_vill 
replace max_tribe_vill=15 if p_tribe15==biggest_tribe_vill 
replace max_tribe_vill=16 if p_tribe16==biggest_tribe_vill 
replace max_tribe_vill=17 if p_tribe17==biggest_tribe_vill 
replace max_tribe_vill=18 if p_tribe18==biggest_tribe_vill 
replace max_tribe_vill=19 if p_tribe19==biggest_tribe_vill 
replace max_tribe_vill=20 if p_tribe20==biggest_tribe_vill 
replace max_tribe_vill=21 if p_tribe21==biggest_tribe_vill 



gen tribe_code2 = 1 if tribe_name=="Afridi"
replace tribe_code2 = 2 if tribe_name=="Bangash"
replace tribe_code2 = 3 if tribe_name=="Bhittani"
replace tribe_code2 = 4 if tribe_name=="Burki"
replace tribe_code2 = 5 if tribe_name=="Chamkani"
replace tribe_code2 = 6 if tribe_name=="Dawar"
replace tribe_code2 = 7 if tribe_name=="Jaji"
replace tribe_code2 = 8 if tribe_name=="Kharoti"
replace tribe_code2 = 9 if tribe_name=="Mangal"
replace tribe_code2 = 10 if tribe_name=="Mehsud"
replace tribe_code2 = 11 if tribe_name=="Mohmand"
replace tribe_code2 = 12 if tribe_name=="Muqbal"
replace tribe_code2 = 13 if tribe_name=="Orakzai"
replace tribe_code2 = 14 if tribe_name=="Safi"
replace tribe_code2 = 15 if tribe_name=="Sherani"
replace tribe_code2 = 16 if tribe_name=="Shinwari"
replace tribe_code2 = 17 if tribe_name=="Tarkani"
replace tribe_code2 = 18 if tribe_name=="Turi"
replace tribe_code2 = 19 if tribe_name=="UtmanKhel"
replace tribe_code2 = 20 if tribe_name=="Wazir"
replace tribe_code2 = 21 if tribe_name=="Zaimukht"


gen minority_vill = 1 
replace minority_vill = 0 if max_tribe_vill==tribe_code2 



foreach var of varlist p_tribe1 p_tribe2 p_tribe3 p_tribe4 p_tribe5 p_tribe6 p_tribe7 p_tribe8 p_tribe9 p_tribe10 p_tribe11 p_tribe12 p_tribe13 p_tribe14 p_tribe15 p_tribe16 p_tribe17 p_tribe18 p_tribe19 p_tribe20 p_tribe21 {
gen p`var' = `var'*100
}

* Fractionalization index (VILLAGE)
foreach var of varlist pp_tribe1 pp_tribe2 pp_tribe3 pp_tribe4 pp_tribe5 pp_tribe6 pp_tribe7 pp_tribe8 pp_tribe9 pp_tribe10 pp_tribe11 pp_tribe12 pp_tribe13 pp_tribe14 pp_tribe15 pp_tribe16 pp_tribe17 pp_tribe18 pp_tribe19 pp_tribe20 pp_tribe21 {
gen one_minus_`var' = 100-`var'
gen multipl_`var' = `var'*one_minus_`var'
}
egen temp_tribal_frac=rowtotal(multipl_*), missing
gen tribal_frac0 = temp_tribal_frac/10000
drop temp_* one_minus* multipl_*
label variable tribal_frac0 "Tribal fractionalization (village)"



drop p_tribe1 p_tribe2 p_tribe3 p_tribe4 p_tribe5 p_tribe6 p_tribe7 p_tribe8 p_tribe9 p_tribe10 p_tribe11 p_tribe12 p_tribe13 p_tribe14 p_tribe15 p_tribe16 p_tribe17 p_tribe18 p_tribe19 p_tribe20 p_tribe21 pp_tribe1 pp_tribe2 pp_tribe3 pp_tribe4 pp_tribe5 pp_tribe6 pp_tribe7 pp_tribe8 pp_tribe9 pp_tribe10 pp_tribe11 pp_tribe12 pp_tribe13 pp_tribe14 pp_tribe15 pp_tribe16 pp_tribe17 pp_tribe18 pp_tribe19 pp_tribe20 pp_tribe21 

bys vill_code: gen village=_n



******************************
****     TEHSIL MINORITY  ****
******************************


bys tehs_code: egen p_tribe1 = mean(tribe1) 
bys tehs_code: egen p_tribe2 = mean(tribe2) 
bys tehs_code: egen p_tribe3 = mean(tribe3) 
bys tehs_code: egen p_tribe4 = mean(tribe4) 
bys tehs_code: egen p_tribe5 = mean(tribe5) 
bys tehs_code: egen p_tribe6 = mean(tribe6) 
bys tehs_code: egen p_tribe7 = mean(tribe7) 
bys tehs_code: egen p_tribe8 = mean(tribe8) 
bys tehs_code: egen p_tribe9 = mean(tribe9) 
bys tehs_code: egen p_tribe10 = mean(tribe10)
bys tehs_code: egen p_tribe11 = mean(tribe11)
bys tehs_code: egen p_tribe12 = mean(tribe12)
bys tehs_code: egen p_tribe13 = mean(tribe13)
bys tehs_code: egen p_tribe14 = mean(tribe14)
bys tehs_code: egen p_tribe15 = mean(tribe15)
bys tehs_code: egen p_tribe16 = mean(tribe16)
bys tehs_code: egen p_tribe17 = mean(tribe17)
bys tehs_code: egen p_tribe18 = mean(tribe18)
bys tehs_code: egen p_tribe19 = mean(tribe19)
bys tehs_code: egen p_tribe20 = mean(tribe20)
bys tehs_code: egen p_tribe21 = mean(tribe21)



bys tehs_code: gen biggest_tribe_tehs = max(p_tribe1, p_tribe2,  p_tribe3,  p_tribe4,  p_tribe5,  p_tribe6,  p_tribe7,  p_tribe8,  p_tribe9,  p_tribe10,  p_tribe11,  p_tribe12,  p_tribe13,  p_tribe14,  p_tribe15,  p_tribe16,  p_tribe17,  p_tribe18,  p_tribe19,  p_tribe20,  p_tribe21)

gen max_tribe_tehs=1 if p_tribe1==biggest_tribe_tehs 
replace max_tribe_tehs=2 if p_tribe2==biggest_tribe_tehs 
replace max_tribe_tehs=3 if p_tribe3==biggest_tribe_tehs 
replace max_tribe_tehs=4 if p_tribe4==biggest_tribe_tehs 
replace max_tribe_tehs=5 if p_tribe5==biggest_tribe_tehs 
replace max_tribe_tehs=6 if p_tribe6==biggest_tribe_tehs 
replace max_tribe_tehs=7 if p_tribe7==biggest_tribe_tehs 
replace max_tribe_tehs=8 if p_tribe8==biggest_tribe_tehs 
replace max_tribe_tehs=9 if p_tribe9==biggest_tribe_tehs 
replace max_tribe_tehs=10 if p_tribe10==biggest_tribe_tehs 
replace max_tribe_tehs=11 if p_tribe11==biggest_tribe_tehs 
replace max_tribe_tehs=12 if p_tribe12==biggest_tribe_tehs 
replace max_tribe_tehs=13 if p_tribe13==biggest_tribe_tehs 
replace max_tribe_tehs=14 if p_tribe14==biggest_tribe_tehs 
replace max_tribe_tehs=15 if p_tribe15==biggest_tribe_tehs 
replace max_tribe_tehs=16 if p_tribe16==biggest_tribe_tehs 
replace max_tribe_tehs=17 if p_tribe17==biggest_tribe_tehs 
replace max_tribe_tehs=18 if p_tribe18==biggest_tribe_tehs 
replace max_tribe_tehs=19 if p_tribe19==biggest_tribe_tehs 
replace max_tribe_tehs=20 if p_tribe20==biggest_tribe_tehs 
replace max_tribe_tehs=21 if p_tribe21==biggest_tribe_tehs 


gen minority_tehs = 1 
replace minority_tehs = 0 if max_tribe_tehs==tribe_code2 


drop p_tribe1 p_tribe2 p_tribe3 p_tribe4 p_tribe5 p_tribe6 p_tribe7 p_tribe8 p_tribe9 p_tribe10 p_tribe11 p_tribe12 p_tribe13 p_tribe14 p_tribe15 p_tribe16 p_tribe17 p_tribe18 p_tribe19 p_tribe20 p_tribe21 

bys agency_code: gen agenc=_n


drop tribe1 tribe2 tribe3 tribe4 tribe5 tribe6 tribe7 tribe8 tribe9 tribe10 tribe11 tribe12 tribe13 tribe14 tribe15 tribe16 tribe17 tribe18 tribe19 tribe20 tribe21 



******************************
****       OUTCOMES       ****
******************************


recode taxes 1=4 2=3 3=2 4=1 5=0 6=., gen(taxes1)
recode formal_justice 1=4 2=3 3=2 4=1 5=0 6=., gen(formal_justice1)
recode policing 1=4 2=3 3=2 4=1 5=0 6=., gen(policing1)


zval tribal_frac0 taxes1 formal_justice1 policing1 
label variable z_tribal_frac0 "Tribal fractionalization (village)"


egen enforcement1 = rowmean(formal_justice1 policing1)
zval enforcement1 



save "${PathData}fata_data_min_recoded.dta", replace

